package com.example.myapplication.utils;

import android.annotation.SuppressLint;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
 * json处理工具
 */
public final class JacksonUtils {

    //时间序列化时变为时间戳
    static class LocalDateTimeToLongSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException, JsonProcessingException {
            jsonGenerator.writeNumber(value.getTime());
        }
    }

    //时间戳反序列化时间
    static class LongToLocalDateTimeDeserializer extends JsonDeserializer<Date> {

        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return new Date(jsonParser.getLongValue());
        }
    }

    //时间序列化时变为字符串
    static class LocalDateTimeToStringSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException, JsonProcessingException {
            jsonGenerator.writeNumber('\"'+format.format(value)+'\"');
        }
    }

    //时间戳反序列化时间
    static class StringToLocalDateTimeDeserializer extends JsonDeserializer<Date> {

        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            try {
                return format.parse(jsonParser.getValueAsString());
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
            return null ;
        }
    }

    private static final ObjectMapper objectMapper;
    @SuppressLint("SimpleDateFormat")
    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS") ;
    static {
        JsonFactory factory = new JsonFactory();
        factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        objectMapper = new ObjectMapper();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        objectMapper.setDateFormat(format);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        SimpleModule timeModule = new SimpleModule();
//       timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeToLongSerializer());
//        timeModule.addDeserializer(LocalDateTime.class, new LongToLocalDateTimeDeserializer());
        timeModule.addSerializer(Date.class, new LocalDateTimeToStringSerializer());
        timeModule.addDeserializer(Date.class, new StringToLocalDateTimeDeserializer());
        objectMapper.registerModule(timeModule);
    }
    public static ObjectMapper objectMapper() {
        return objectMapper;
    }
}
